/*********************************************************************
Replication code for Systemic Discrimination Among Large U.S. Employers
Patrick M. Kline, Evan K. Rose, Christopher R. Walters
April, 2022

This code produces inputs used in the creation of Table 7, which is 
requires matlab code (see table7a.m).

It requires top-level directory be set to the replication folder using
the global below.
*********************************************************************/

global dir "/accounts/projects/pkline/randres/randres/replication"
capture restore
clear all
set seed 126312

* Load the data
use ${dir}/data/data.dta, clear

* Collapse to job difs for relevant groups
foreach grp of varlist black female over40 {
    foreach var of varlist st soc3 {
        preserve
        collapse (mean) cb, by(job_id firm_id `var' `grp')
        qui reshape wide cb, i(job_id firm_id `var') j(`grp')
        qui gen cb = cb1 - cb0
        qui drop if cb == .

        * Save for KSS routine
        keep `var' firm_id cb 
        order `var' firm_id cb
        sort  `var' firm_id 
        outsheet using ${dir}/code/kss/data/`grp'_`var'_firm.csv, comma replace nonames nolabel
        restore
    }
}
